package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.exception.BusinessException;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.List;


@Transactional
@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public void save(Employee employee, Long[] ids) {

        //插入员工对象
        employeeMapper.insert(employee);
        //如果ids不为null,且数组长度大于0的时候
        // 将员工对象和角色信息的关系放在中间表里
        if (ids != null && ids.length > -1) {
            for (Long rId : ids) {
                employeeMapper.insertRelation(employee.getId(), rId);
            }
        }
    }

    @Override
    public void delete(Long id) {
        //删除了该员工的数据
        employeeMapper.deleteByPrimaryKey(id);
        //顺便把中间表的数据删了
        employeeMapper.deleteByeId(id);
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        employeeMapper.deleteByeId(employee.getId());
//        删除原先的关系
        if (ids != null) {

            for (Long rid : ids) {
                //        插入新的关系
                employeeMapper.insertRelation(employee.getId(), rid);
            }

        }
    }

    @Override
    public Employee get(Long id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    @Override
    public List<Employee> getList() {
        List<Employee> employees = employeeMapper.selectAll();
        return employees;
    }

    @Override
    public PageInfo<Employee> query(EmployeeQueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Employee> list = employeeMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public Employee login(String username, String password) {

        if (StringUtil.isEmpty(username)) {
            throw new BusinessException("账号不能为空");
        }
        if (StringUtil.isEmpty(password)) {
//           构造器,密码不能为空为msg的
            throw new BusinessException("密码不能为空");
        }

        Employee employee = employeeMapper.selectByUsername(username);
        if (employee == null || !employee.getPassword().equals(password)) {
            throw new BusinessException("账号密码错误");
        }

        return employee;
    }

    @Override
    public List<Permission> getPermissions(Long eId) {
        List<Permission> permissions = employeeMapper.selectByEId(eId);
        return permissions;
    }

    @Override
    public void exportXls(ServletOutputStream outputStream) throws IOException {
        //定义工作表
        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet();
//        定义行
        Row row = sheet.createRow(0);
//        定义单元格
        //        单元格内容
        row.createCell(0).setCellValue("员工编号");
        row.createCell(1).setCellValue("员工姓名");
        row.createCell(2).setCellValue("员工邮箱");
        List<Employee> employees = employeeMapper.selectAll();
        for (int i = 0; i < employees.size(); i++) {
            Employee employee = employees.get(i);
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(employee.getId());
            row.createCell(1).setCellValue(employee.getName());
            row.createCell(2).setCellValue(employee.getEmail());
        }
//        文件输出到当前流浪器
        wb.write(outputStream);
    }

    @Override
    public void insert(Employee employee) {
        employeeMapper.insert(employee);
    }

    @Override
    public void importXls(MultipartFile file) throws IOException {
        HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
        Sheet sheet = wb.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i <= lastRowNum; i++) {
            Row row = sheet.getRow(i);
            Employee employee = new Employee();
            employee.setUsername(row.getCell(0).getStringCellValue());
            employee.setName(row.getCell(1).getStringCellValue());
            double numericCellValue = row.getCell(2).getNumericCellValue();
            employee.setAge((int) numericCellValue);
            employee.setEmail(row.getCell(3).getStringCellValue());
            employee.setAdmin(false);
            employee.setPassword("88888");
            employeeMapper.insert(employee);
        }

    }
}
